﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SiteMonitorLogic
{
    public class Statistics
    {
        ///////// ******************
        // This class should be rewritten to store the samples in some sort of datastore
        ///////// ******************

        //string CurrentLogFile
        //{
        //    get
        //    {
                
        //    }
        //}

        private class Sample
        {
            public string Key { get; set; }
            public DateTime TimeStamp { get; set; }
            public CheckResult Result { get; set; }
        }

        private Dictionary<string, List<Sample>> _stats;

        public void Clear()       
        {
            _stats = null;
        }

        public void Record(string key, CheckResult result)
        {
            if (_stats == null)
                _stats = new Dictionary<string, List<Sample>>();

            if (!_stats.ContainsKey(key))
                _stats.Add(key, new List<Sample>());

            _stats[key].Add(new Sample() { Key = key, TimeStamp = DateTime.Now, Result = result });
        }

        public string[] Keys { get { return _stats.Keys.ToArray(); } }

        public decimal GetPercentUptime(string key)
        {
            return (decimal)(_stats[key].Where(s => s.Result.ResultCode == CheckResultCode.Success).Count() * 100)
                / _stats[key].Count;
        }

        public decimal GetPercentUptime()
        {
            decimal tot = 0;
            foreach (string key in _stats.Keys)
            {
                tot += GetPercentUptime(key);
            }
            return tot / _stats.Count;
        }
    }
}
